package com.huatai.user.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.huatai.common.api.R;
import com.huatai.user.entity.User;
import com.huatai.user.entity.UserCheckRecord;
import com.huatai.user.service.UserCheckRecordService;
import com.huatai.user.service.UserService;
import com.huatai.user.vo.UserParamInfo;
import com.huatai.user.vo.UserRegisterVo;
import com.huatai.user.vo.UserVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @program: nanming-saas
 * @description: 客户端用户接口
 * @author: songxiude
 * @create: 2024-04-22 16:13
 **/
@RestController
@RequestMapping("/clientuser")
@Api(value = "客户端用户管理", tags = "客户端用户管理")
public class ClientUserController {

	@Autowired
	private UserService userService;

	@Autowired
	private UserCheckRecordService checkRecordService;

	@PostMapping("/getUserList")
	@ApiOperation(value = "获取客户端用户列表", notes = "获取客户端用户列表")
	@ApiOperationSupport(order = 2, includeParameters = {
		"userParamInfo.account",
		"userParamInfo.phone",
		"userParamInfo.checkState",
		"userParamInfo.page",
		"userParamInfo.size"
	})
	public R getUserList(@RequestBody UserParamInfo userParamInfo){
		//只查客户端用户
		userParamInfo.setUserType(2);
		return R.data(userService.getUserList(userParamInfo));
	}

	@PostMapping("/addUser")
	@ApiOperation(value = "添加用户端用户", notes = "添加用户端用户")
	@ApiOperationSupport(order = 1, includeParameters = {
		"user.account",
		"user.phone"
	})
	public R addUser(@RequestBody User user){
		return R.data(userService.addUser(user));
	}

	@PostMapping("/stopUser")
	@ApiOperation(value = "禁用用户", notes = "禁用用户")
	public R stopUser(@ApiParam(name = "uid",value = "用户ID",required = true) @RequestParam(name = "uid") Long uid){
		return R.status(userService.stopUser(uid));
	}

	@PostMapping("/userPwReset")
	@ApiOperation(value = "客户端用户密码重置", notes = "客户端用户密码重置")
	public R userPwReset(@ApiParam(name = "uid",value = "用户ID",required = true) @RequestParam(name = "uid") Long uid){
		return R.data(userService.licentUserPwReset(uid));
	}

	@PostMapping("/userPasswordEdit")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "orig_password", value = "原始密码", required = true),
		@ApiImplicitParam(name = "new_password", value = "新密码", required = true),
	})
	@ApiOperation(value = "用户修改密码(客户端)", notes = "用户修改密码(客户端)")
	//@PreAuthorize("hasAuthority('user:pass:edit')")
	public R userPasswordEdit(@NotBlank(message = "原始密码不能为空") @RequestParam("orig_password") String origPassword,
							  @NotBlank(message = "新密码不能为空") @RequestParam("new_password") String newPassword) {
		return R.status(userService.clientUserPasswordEdit(origPassword, newPassword));
	}

	@PostMapping("/checkUser")
	@ApiOperation(value = "客户端用户审核", notes = "客户端用户审核")
	public R checkUser(@RequestBody UserCheckRecord userCheckRecord){
		if(checkRecordService.checkUser(userCheckRecord)){
			return R.success("审核操作成功");
		}
		return R.fail("审核操作失败");
	}

	@GetMapping("/getUserCheckList")
	@ApiOperation(value = "获取用户审核记录", notes = "获取用户审核记录")
	public R<List<UserCheckRecord>> getUserCheckList(@ApiParam(value = "用户id",name = "user_id",required = true) @RequestParam(name = "user_id") Long userId){
		List<UserCheckRecord> list = checkRecordService.list(Wrappers.<UserCheckRecord>lambdaQuery().eq(UserCheckRecord::getUserId, userId));
		List<UserCheckRecord> recordList = Optional.ofNullable(list).orElse(Collections.emptyList());
		return R.data(recordList);
	}

	@PostMapping("/userRegister")
	@ApiOperation(value = "用户注册", notes = "用户注册")
	public R userRegister(@Valid @RequestBody UserRegisterVo user){
		if(userService.userRegister(user)){
			return R.success("用户注册成功");
		}
		return R.fail("用户注册失败");
	}
}
